package com.rzico.sbl.ui.report.driver

import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.RCouponData
import com.rzico.sbl.ui.report.ReportOrderDetailActivity
import com.rzico.sbl.viewmodel.ReportStatisticViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class DriverCouponRecordActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<ReportStatisticViewModel>()

    override fun initData() {
        initTitle(title = "订单明细")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关使用记录！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            if (!isLoadingMore) {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                        }
                    )

                    view.addItemDecoration(SpaceTBDecoration())

                    mAdapter = SlimAdapter.creator()
                        .register<RCouponData>(R.layout.item_report_statistic_coupon_2) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_coupon_sn, bean.sn)
                                text(R.id.item_coupon_time, bean.createDate.getTimeString())
                                text(
                                    R.id.item_coupon_num1,
                                    bean.couponDiscount.formatInteger()
                                )
                                text(
                                    R.id.item_coupon_num2,
                                    bean.vipDiscount.formatInteger()
                                )
                                text(
                                    R.id.item_coupon_num3,
                                    bean.exchangeDiscount.formatInteger()
                                )

                                clicked {
                                    startActivityEx<ReportOrderDetailActivity>(
                                        "orderSn" to bean.sn
                                    )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    /**
     * 代金券统计列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .couponDetail(
                page = index,
                shopId = intent.getExtra("shopId"),
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}